import { View,Image,Text,Button } from "@tarojs/components";
import Taro from "@tarojs/taro";
import {AtIcon} from 'taro-ui';
import {useState} from 'react';
import { addFavoriteCompany, } from "../../api/mode";
import bigHouse from '../../images/bigHouse.png'
import './index.scss'

export default function CompanyCard(props){

  // console.log(props.Info);
  // const chooseCompany=(id)=>{
  //   switch(id){
  //     case "0": return('国企')
  //     case "1": return('央企')
  //     case "2": return('事业单位')
  //     case "3": return('政府机关')
  //     case "4": return('民营企业')
  
  //   }
  // }
  // const [favoriteIsOpen,setFavoriteIsOpen]=useState(false)
  const [isCollected,setIsCollected]=useState(props.isCollected?props.isCollected:false)  
  const companyInfo=props.Info;
  const companyId=companyInfo.id
  const isSpecial=props.isSpecial?props.isSpecial:0//是否是专场招聘

  const detailInfo=()=>{
    Taro.navigateTo({
      url:`/pages/companyDetail/companyDetail?companyId=${companyId}&isSpecial=${isSpecial}`
    })
  }

  const openid=Taro.getStorageSync(`openid`)
  const submitCompanyItem={
    openid:openid,
    companyId:companyId,
    positionId:0
  }

  const submitMyFavoriteCompany=()=>{
    if(Taro.getStorageSync(`openid`)){
      addFavoriteCompany(submitCompanyItem).then(res=>{
        console.log('收藏成功',res);
        setIsCollected(!isCollected)//收藏图标切换
        // setFavoriteIsOpen(!favoriteIsOpen)
        if(!isCollected){
          Taro.showToast({
            title:'收藏成功',
            icon:'success',
            duration:1000
          })
        }
      })
    }else{
      Taro.showToast({
        title: '请先登录',
      icon: 'error',
      duration: 1500
      })
    }

  }
  return(
    <View className='companyCard' >

          <View className='at-row'>
          <View className='at-col at-col-7'>
               <View className='companyName'>
            {/* <AtIcon value='home' size='20' color='#000'></AtIcon> */}
            <View className='imgBox'> <Image className='bigHouse' src={bigHouse}></Image></View>
                    {companyInfo.companyName}
               </View>


               <View className='companyAddrAndType'>
               <Text>{companyInfo.address} | </Text>
               <Text> {companyInfo.type}</Text>
               </View>

          <View className='companyCardFeature at-row at-row--wrap'>
          {companyInfo.feature? companyInfo.feature.map((text,index)=>(<View className='companyFeature_item '  key={index}><Text >{text}</Text></View>)):null}
          </View>

          </View>
            <Image className='companyImage at-col at-col-5' src={'data:image/png;base64,'+companyInfo.photo} />
          </View>

          <View className='CompanyCardBottom' >
          <AtIcon  value={isCollected?'star-2':'star'} size='30' color='#4C80AE' onClick={submitMyFavoriteCompany}></AtIcon>
          {/* <AtToast  isOpened text='收藏成功' icon='check'></AtToast> */}

          <Button className='detailButton' onClick={detailInfo}>查看详情</Button>
          </View>
    </View>
  )
}